package com.ryskoo.catering.ordermain.repository;

import com.ryskoo.catering.ordermain.domain.OrderMainPO;
import com.ryskoo.common.dto.customer.VendorIndexInfo;
import com.ryskoo.common.hclconstant.OrderStateEnum;
import com.ryskoo.framework.core.bean.QueryConditionEnum;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.repository.BasicJPAImpl;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

@Repository
public class OrderMainJPAImpl extends BasicJPAImpl<OrderMainPO> implements IOrderMainRepository{

    /**
     * 根据订单编号获取订单
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderMainPO getByOrderId(String orderId) {
        QueryMessageDTO qm = new QueryMessageDTO();
        qm.addCondition("entityNo", QueryConditionEnum.EQ.name(), orderId);
        List<OrderMainPO> list = this.queryByCondition(qm, false);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public double getOrderAmtByDate(String date,String vendorUuid) {
        String sql = "select sum(o.pay_price) from cater_order_main o where o.vendor_uuid = :vendorUuid and substr(o.create_time,1,10) = :date and o.state not in ('1','-1')";
        SQLQuery query = getH4Session().createSQLQuery(sql);
        query.setString("vendorUuid",vendorUuid);
        query.setString("date",date);
        Object obj = query.uniqueResult();
        if(null!=obj){
            return Double.parseDouble(obj.toString());
        }
        return 0;
    }

    @Override
    public VendorIndexInfo getDailyVendorIndexInfo(String vendorUuid, String date) {
        String sql = "select vendor_uuid as vendorUuid, SUBSTR(create_time, 1, 10) as date,"
                + " SUM(pay_price) as income, COUNT(uuid) as quantity, SUM(person_num) as people"
                + " from cater_order_main WHERE state!=1 and state != -1"
                + " and vendor_uuid = :vendorUuid";
        if (!StringUtils.isEmpty(date)) {
        	sql += " and SUBSTR(create_time, 1, 10) = :date";
        }
        Query query = getH4Session().createSQLQuery(sql).addScalar("vendorUuid", StandardBasicTypes.STRING)
                .addScalar("date", StandardBasicTypes.STRING)
                .addScalar("income", StandardBasicTypes.DOUBLE)
                .addScalar("quantity", StandardBasicTypes.INTEGER)
                .addScalar("people", StandardBasicTypes.INTEGER)
                .setResultTransformer(Transformers.aliasToBean(VendorIndexInfo.class));
        query.setString("vendorUuid", vendorUuid);
        
        if (!StringUtils.isEmpty(date)) {
        	 query.setString("date", date);
        }
        
        return (VendorIndexInfo) query.uniqueResult();
    }

    /**
     * 查询商品月销数量
     *
     * @param products
     * @param todayDateStr
     * @param monthBeforeDateStr
     * @return
     */
    @Override
    public List<Object[]> getMonthSellNum(List<String> products, String todayDateStr, String monthBeforeDateStr) {
        String sql = "select d.product_uuid,SUM(d.buy_num) from cater_order_main o, cater_order_detail d where o.uuid=d.order_uuid and o.state=:state and o.create_time>:beginTime and o.create_time<=:endTime and d.product_uuid in (:products) GROUP BY d.product_uuid";
        SQLQuery query = getH4Session().createSQLQuery(sql);
        query.setInteger("state",OrderStateEnum.FINISH.getValue());
        query.setString("beginTime",monthBeforeDateStr);
        query.setString("endTime",todayDateStr);
        query.setParameterList("products", products);
        List<Object[]> list = query.list();
        return list;
    }

    /**
     * 查询1个月内商户月销商品数量
     *
     * @param vendorUuid
     * @param todayDateStr
     * @param monthBeforeDateStr
     * @return
     */
    @Override
    public int getMonthSellProductNum(String vendorUuid, String todayDateStr, String monthBeforeDateStr) {
        String sql = "select SUM(d.buy_num) from cater_order_main o, cater_order_detail d where o.uuid=d.order_uuid and o.vendor_uuid=:vendorUuid and o.state=:state and o.create_time>:beginTime and o.create_time<=:endTime";
        SQLQuery query = getH4Session().createSQLQuery(sql);
        query.setString("vendorUuid", vendorUuid);
        query.setInteger("state",OrderStateEnum.FINISH.getValue());
        query.setString("beginTime",monthBeforeDateStr);
        query.setString("endTime",todayDateStr);
        Object obj = query.uniqueResult();
        if(obj != null){
            return ((BigDecimal)obj).intValue();
        }
        return 0;
    }

    @Override
    public int getOrderCountByDate(String date,String vendorUuid) {
        String sql = "select count(o.uuid) from cater_order_main o where o.vendor_uuid = :vendorUuid and substr(o.create_time,1,10) = :date";
        SQLQuery query = getH4Session().createSQLQuery(sql);
        query.setString("vendorUuid",vendorUuid);
        query.setString("date",date);
        Object obj = query.uniqueResult();
        if(null!=obj){
            BigInteger bi = (BigInteger)obj;
            return bi.intValue();
        }
        return 0;
    }
}
